﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace Firestarter.DB.Schema.UI.RevEngDBWizard
{
    public class FileExtensionsFilterFormatter
    {
        private readonly IList<FileExtensionFilterElement> _fileExtensionElements = new List<FileExtensionFilterElement>();
        private string _extensionFilter;


        public ReadOnlyCollection<FileExtensionFilterElement> FileExtensionElements
        {
            get { return new ReadOnlyCollection<FileExtensionFilterElement>(_fileExtensionElements); }

        }

        public string ExtensionFilter
        {
            get { return _extensionFilter; }
        }

        public void AddExtensionFilter(string extension, string description)
        {
            _fileExtensionElements.Add(new FileExtensionFilterElement() { Extension = extension, Description = description });
        }


        public void Format(bool withAllFilesFilter = true)
        {
            StringBuilder sb = new StringBuilder("");
            foreach (FileExtensionFilterElement element in _fileExtensionElements)
            {
                if (sb.Length==0)
                {
                    element.Format();
                    sb.Append(element.ExtensionFilter);
                    continue;
                }
                sb.Append("|");
                element.Format();
                sb.Append(element.ExtensionFilter);

            }
            if (withAllFilesFilter)
            {
                if (sb.Length != 0) sb.Append("|");
                sb.Append("All Files (*.*)|*.*");
            }


            _extensionFilter = sb.ToString();
        }
    }
}